package horseParkMonitor.configuration;

import myFiler.MyFiler;

import javax.swing.text.AbstractDocument;
import javax.swing.text.Element;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Oxana on 1/3/15.
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement( name ="dispensing" )
public class Dispensing {
    @XmlElement( name = "bill", type =Bill.class )
    private List<Bill> billList =new ArrayList<Bill>();

    public Dispensing() {
    }

    public Dispensing(List<Bill> billList) {
        this.billList = billList;
    }

    public void setBillList(List<Bill> billList) {
        this.billList = billList;
    }

    public List<Bill> getBillList() {

        return billList;
    }
    public static final String configurationFileName= MyFiler.getCurrentDirectory()+ File.separator+"configuration"+File.separator+
            "dispensing_info.xml";

    // Export
    public static void marshal(List<Bill> ids, File selectedFile)
            throws IOException, JAXBException {
        JAXBContext context;
        BufferedWriter writer = null;
        writer = new BufferedWriter(new FileWriter(selectedFile));
        context = JAXBContext.newInstance(Dispensing.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(new Dispensing(ids) , writer);
        writer.close();
    }

    // Import
    public static List<Bill> unmarshal(File importFile) throws JAXBException {
        Dispensing ids = new Dispensing();

        JAXBContext context = JAXBContext.newInstance(Dispensing.class);
        Unmarshaller um = context.createUnmarshaller();
        ids = (Dispensing) um.unmarshal(importFile);

        return ids.getBillList();
    }
    public static String getConfigurationFileName() {
        return configurationFileName;
    }

}
